package com.develop.mybluetooth.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter4.BaseQuickAdapter
import com.develop.bluetoothlib.entity.MyBluetoothDevice
import com.develop.mybluetooth.databinding.ItemScanBinding
import com.develop.mybluetooth.utils.Tools

class ScanAdapter : BaseQuickAdapter<MyBluetoothDevice, ScanAdapter.VH>() {

    // 自定义ViewHolder类
    class VH(
        parent: ViewGroup,
        val binding: ItemScanBinding = ItemScanBinding.inflate(
            LayoutInflater.from(parent.context), parent, false
        ),
    ) : RecyclerView.ViewHolder(binding.root)

    override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): VH {
        // 返回一个 ViewHolder

        return VH(parent)
    }

    override fun onBindViewHolder(holder: VH, position: Int, item: MyBluetoothDevice?) {
        // 设置item数据


        holder.binding.tvName.text = item?.name
        holder.binding.tvAddress.text = item?.address

        val scanRecord = item?.scanRecord
        if (scanRecord != null && scanRecord.isNotEmpty()) {
            holder.binding.tvRecord.visibility = View.VISIBLE
            holder.binding.tvRecord.text = Tools.bytesToHexString(scanRecord)
        } else {
            holder.binding.tvRecord.visibility = View.GONE
        }
    }

}